G = [['A', 'B', '/', 'D'],
     ['E', 'F', '/', 'H'],
     ['I', 'J', '/', '/'],
     ['M', 'N', 'O', 'P']]

V = [[0] * len(G) for i in range(len(G[0]))]
print(V)
fx = [(1, 0), (0, 1), (-1, 0), (0, -1)]

n, m = len(G), len(G[0])
import collections


def bfs(ex, ey):
    Q = collections.deque()
    Q.append((ex, ex))
    V[ex][ex] = 1
    while Q:
        hx, hy = Q[0]
        for d in fx:
            nx, ny = hx + d[0], hy + d[1]
            if nx < 0 or nx >= n or ny < 0 or ny >= m or V[nx][ny] == 1 or G[nx][ny] == '/': continue
            Q.append((nx, ny))
            V[nx][ny] = 1
        print(G[hx][hy], end="")

        Q.popleft()


bfs(0, 0)
